from docx import Document
from openpyxl import Workbook
import numpy as np
import os
import re
parentpath = "/home/s/Public/jyzj"
sonpath = [            #各个分类子目录
    "/基础/",
    "/免疫/",
    "/血液/",
    "/微生物/",
    "/质量/",
    "/生化/"
]
volid_head = r"^[0-9A-E]+、|^[A-E] 、|^<[0-9]>+ 、|^[A-E]."
def shai(string):
    return re.match("^[0-9]+、",string)
def type1(z):
    pass
def type2(z):
    pass
def type3(z):
    pass
# typeall = {
#     "a1_a2":type1,
#     "a3_a4":type2,
#     "b":type3
# }
wb = Workbook()
booksheet = wb.active
def work(inname):
    qs = []             #最终完成的列表，等待写入表格
    question = []       #取出问题
    anwser = []         #取出答案
    exp = []            #取出解析
    question_head = []  #Ｂ题型的答案列表
    indexA1 = 0         #A1题型的标记
    indexA2 = 0         #A2题型的标记
    indexA3A4 = 0       #A3/A4题型的标记
    indexB = 0          #B题型的标记
    mark = 0
    anwser_yes = False  #答案部分与问题部分的分割标记
    # mark_3 = False
    head_B = False      #B型题题头标记
    head_A3A4 = ""      #A3/A4题型的题头
    docx = inname.paragraphs
    for page,p in enumerate(docx):
        z = p.text
        if ("一、A1" in z) or ("二、A1" in z):
            mark = 1
        if ("一、A2" in z) or ("二、A2" in z):
            mark = 2 
        if "A3/A4" in z:
            mark = 3 
        if ("三、B" in z) or ("四、B" in z) or ("二、B" in z) or ("一、B" in z):
            mark = 4 
        if "答案部分" in z:
            anwser_yes = True
        if anwser_yes:
            if "正确答案" in p.text:
                x = p.text.strip("【正确答案】")
                anwser.append(x)
            elif "答案解析" in p.text:
                y = p.text.strip("【答案解析】")
                exp.append(y)
                # docx[page:page+3].clear()
                # continue

        elif mark == 1:
            if indexA1 > 0:
                
                z = re.sub(volid_head,'',z)
                question.append(z)
            if (indexA1 % 6 == 0) and (question != []):
                qs.append(question)
                # print(question)
                question = []
            indexA1 += 1
        elif mark == 2:
            if indexA2 > 0:
                z = re.sub(volid_head,'',z)
                question.append(z)
            if (indexA2 % 6 == 0) and (question != []):
                qs.append(question)
                # print(question)
                question = []
            indexA2 += 1
        elif mark == 3:
            if "<1> 、" in z:
                head_A3A4 = docx[page-1].text
                indexA3A4 = 1
                z = re.sub(volid_head,'',z)
                question.append(z)
            zz = shai(z)
            if zz != None:
                indexA3A4 = 0
            #     mark_3 = True
            # else:
            #     mark_3 = False
            if indexA3A4 > 1:
                z = re.sub(volid_head,'',z)
                question.append(z)
            if (indexA3A4 % 6 == 0) and (question != []):
                head_A3A4 = re.sub(volid_head,'',head_A3A4)
                question = [head_A3A4+question[0]]+question[1:]
                # print(question)
                qs.append(question)
                question = []
            indexA3A4 += 1
        elif mark == 4:
            zz = shai(z)
            if zz != None:
                head_B = True
            else:
                head_B = False
            if head_B:
                z = z[list(zz.span())[1]:]
                question_head = z.split("\n")
                question_head = [re.sub(volid_head,"",i) for i in question_head]
                indexB = 1
            if indexB % 6 == 2:
                z = re.sub(volid_head,'',z)
                question.append(z)
                question = question + question_head
                # print(question)
                qs.append(question)
                question = []
            indexB += 1
    return qs,anwser,exp
# work(infile,"test.xlsx")
for k in sonpath:
    head = [
        "问题",
        "A",
        "B",
        "C",
        "D",
        "E",
        "答案",
        "解析",
    ]
    booksheet.append(head)
    f = parentpath + k +"习题/2/"
    o = parentpath + k +"习题/3/"
    # os.mkdir(o)
    m = [i[2] for i in os.walk(f)]
    for x in m[0]:
        filename = f+x
        infilename = Document(filename)
        # print(outfilename)
        qs,anwser,exp = work(infilename)
        for d,i in enumerate(qs):
            # print(anwser)
            try:
                i.append(anwser[d])
                i.append(exp[d])
            except IndexError:
                # print("jixu")
                pass
            booksheet.append(i)
    outname = parentpath + k.strip("/") + ".xlsx"
    wb.save(outname)